var express = require('express');
var router = express.Router();
const sid = require('shortid')

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync(__dirname + '/../data/db.json')
const db = low(adapter)

/* GET home page. */
router.get('/account', function(req, res, next) {
  let accounts = db.get('accounts').value()
  //console.log(accounts)
  res.render('list',{accounts})

});

router.get('/account/create', function(req, res, next) {

  res.render('create')
});

router.post('/account', (req,res) => {
  let id = sid.generate()
  db.get('accounts').unshift({id:id,...req.body}).write();

  res.render('success',{mss:'添加成功！',url:'/account'})
})

router.get('/account/:id', (req,res) => {
  
  let {id} = req.params;

  db.get('accounts').remove({id:id}).write()

  res.render('success',{mss:'删除成功！',url:'/account'})
})




module.exports = router;
